package dao;

import entity.ClassifyInfo;
import openConn.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassifyDao {

    public static void insert(String className) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "insert into classify_info(classify_name) values (?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,className);
        int count = statement.executeUpdate();
        statement.close();
        connection.close();
    }

    public static void update(int classId,String className) throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "update classify_info set classify_name = ? where classify_id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,className);
        statement.setInt(2,classId);
        int count = statement.executeUpdate();
        statement.close();
        connection.close();
    }

    public static List<ClassifyInfo> select() throws SQLException {
        Connection connection = DruidUtils.openConn();
        String sql = "select * from classify_info;";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        List<ClassifyInfo> classifyInfoList = new ArrayList<>();
        while (resultSet.next()){
            int classifyId = resultSet.getInt("classify_id");
            String className= resultSet.getString("classify_name");
            ClassifyInfo classifyInfo =new ClassifyInfo(classifyId,className);
            classifyInfoList.add(classifyInfo);
        }
        resultSet.close();
        statement.close();
        connection.close();
        return classifyInfoList;
    }
}
